﻿using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine;
using PloobsEngine.Cameras;
using PloobsEngine.Input;
using Microsoft.Xna.Framework;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Features;
using PloobsEngine.Commands;
using PloobsEngineDemo3;
using PloobsEngine.Light;
using PloobsEngine.Draw;
using PloobsEngine.Utils;

namespace Etapa1.Screens
{
    /// <summary>
    /// Bepu Physic Screen
    /// </summary>
    public class CullerScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;
        FPSCounter counter;

        public CullerScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);
            counter = new FPSCounter(es);
        }
        
        public override void LoadContent(EngineStuff es)
        {            
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            engine.AddComponent(counter);

            base.LoadContent(es);            

            #region Models

            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cubo", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(100, 20, 0), 1, 5,new Vector3(100, 5, 100));
                NormalDeferred shader = new NormalDeferred();                
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }

            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cubo", "..\\Content\\Textures\\blue");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(90, 30, 0), 1, 10,new Vector3(1));
                NormalDeferred shader = new NormalDeferred();                
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }


            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero, sm);
                NormalDeferred shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 25; j++)
                {
                    IModelo sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\blue");
                    sm.LoadModelo();
                    float px = StaticRandom.RandomBetween(-500, 500);
                    BoxObject pi = new BoxObject(new Vector3(px, 50, j * 10), 1, 5, new Vector3(5));
                    pi.isMotionLess = true;
                    NormalDeferred shader = new NormalDeferred();
                    shader.Initialize();
                    IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                    IObject obj4 = new SimpleObject(mat, pi, sm);
                    mundo.AddObject(obj4);
                }

            }
            

            #endregion            

            cam = new CameraFirstPerson(true);            
            cam.FarPlane = 4000;

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);         

            #region NormalLight
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion            

            //mundo.Culler = new SimpleCuller();
            mundo.Culler = new OctreeCuller(2000,1,5,Vector3.Zero,true);            

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);
            
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            mundo.AddCamera(cam);
            this.World = mundo;

            Drawing.setClearEveryFrame(true);

        }
        public override void Draw(GameTime gameTime)
        {            
            Drawing.Draw2dTextAt2dLocation("FPS Draw " + counter.DrawFPS, new Vector2(40, 20), Color.White);
            Drawing.Draw2dTextAt2dLocation("FPS Update " + counter.UpdateFPS, new Vector2(40, 40), Color.White);
            base.Draw(gameTime);
        }

    }
}

